using System;
using System.Collections.Generic;
using System.Text;

namespace GetTogether.Studio
{
    public class SessionHelper
    {
        public static SessionObjects GetCurrentSession()
        {
            if (System.Web.HttpContext.Current == null) return null;
            SessionObjects sess = (SessionObjects)System.Web.HttpContext.Current.Session[Definition.Session.SESSION_KEY];
            if (sess == null)
            {
                sess = new GetTogether.Studio.SessionObjects();
                sess.Address = System.Web.HttpContext.Current.Request.UserHostAddress.Replace("::1", "127.0.0.1");
                sess.UserCode = sess.Address;
                sess.IsFromSchedule = !string.IsNullOrEmpty(System.Web.HttpContext.Current.Request["from-schedule"]);
                if (Config.Original.EnableLogin)
                {
                    if (System.Web.HttpContext.Current.Request.Cookies != null
                        && System.Web.HttpContext.Current.Request.Cookies.Count > 0
                        && System.Web.HttpContext.Current.Request.Cookies["sign-username"] != null
                        && System.Web.HttpContext.Current.Request.Cookies["sign-password"] != null
                        && System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().IndexOf("login.aspx") < 0)
                    {
                        GetTogether.Studio.Profile.Util.Sign(sess, System.Web.HttpContext.Current.Request.Cookies["sign-username"].Value,
                                System.Web.HttpContext.Current.Request.Cookies["sign-password"].Value);
                    }
                }
                System.Web.HttpContext.Current.Session[Definition.Session.SESSION_KEY] = sess;
            }
            return sess;
        }
    }
}
